package com.yiguo.logistics.web;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;

import com.yiguo.common.Dialogs;
import com.yiguo.logistics.entity.model.ModCommodity;
import com.yiguo.logistics.entity.model.ModOrder;
import com.yiguo.logistics.entity.model.ModPayType;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.util.Log;
import android.util.Xml;

public class LoaderOrderDetails extends AsyncTask<Void, Void, Void>
{
   String url;
   Dialogs dialogs;
   ModOrder modOrder;
   String barCode;
   Intent intent;
   Context context;
   int activityCode;

   public LoaderOrderDetails(String _url, Dialogs _dialogs, String _barCode, Intent _intent, Context _context, int _activityCode)
   {
	  url = _url;
	  dialogs = _dialogs;
	  barCode = _barCode;
	  intent = _intent;
	  context = _context;
	  activityCode = _activityCode;
   }

   private InputStream getInputStream(URL ul)
   {
	  try
	  {

		 return ul.openConnection().getInputStream();
	  }
	  catch (IOException e)
	  {
		 // TODO Auto-generated catch block
		 Log.e("ERROR", "getInputStream()" + e.getMessage());
		 e.printStackTrace();

	  }
	  return null;
   }

   @Override
   protected Void doInBackground(Void... params)
   {
	  // TODO Auto-generated method stub
	  URL ul = null;
	  InputStream is = null;
	  try
	  {
		 ul = new URL(url);
		 is = getInputStream(ul);
		 XmlPullParser parser = Xml.newPullParser();
		 parser.setInput(is, "UTF-8");

		 int eventType = parser.getEventType();

		 ModCommodity modCommodity = null;
		 List<ModCommodity> cList = null;
		 ModPayType modPayType = null;
		 List<ModPayType> pList = null;
		 while (eventType != XmlPullParser.END_DOCUMENT)
		 {
			switch (eventType)
			{
			   case XmlPullParser.START_DOCUMENT:// 文档开始事件,可以进行数据初始化处理
				  modOrder = new ModOrder();
				  modCommodity = new ModCommodity();
				  cList = new ArrayList<ModCommodity>();
				  modPayType = new ModPayType();
				  pList = new ArrayList<ModPayType>();
				  break;

			   case XmlPullParser.START_TAG:// 开始元素事件

				  String name = parser.getName();

				  if (name.equalsIgnoreCase("OrderCode"))
				  {
					 modOrder.setOrderCode(parser.nextText());
				  }
				  else if (name.equalsIgnoreCase("SerialNumber"))
				  {
					 modOrder.setSerialNumber(parser.nextText());
				  }
				  else if (name.equalsIgnoreCase("DeliveryDate"))
				  {
					 modOrder.setDeliveryDate(parser.nextText());
				  }
				  else if (name.equalsIgnoreCase("TotalPrice"))
				  {
					 String s1 = parser.nextText();
					 if (s1.length() > 2)
					 {
						s1 = s1.substring(0, s1.length() - 2);
					 }
					 modOrder.setTotalPrice(s1);
				  }
				  else if (name.equalsIgnoreCase("Consignee"))
				  {
					 modOrder.setConsignee(parser.nextText());
				  }
				  else if (name.equalsIgnoreCase("Address"))
				  {
					 modOrder.setAddress(parser.nextText());
				  }
				  else if (name.equalsIgnoreCase("OrderState"))
				  {
					 modOrder.setOrderState(parser.nextText());
				  }

				  else if (name.equalsIgnoreCase("CommodityName"))
				  {
					 modCommodity = new ModCommodity();
					 modCommodity.setCommodityName(parser.nextText());
				  }
				  else if (name.equalsIgnoreCase("CommodityAmount"))
				  {
					 modCommodity.setCommodityAmount(parser.nextText());
				  }
				  else if (name.equalsIgnoreCase("OrderTotalPrice"))
				  {
					 String s1 = parser.nextText();
					 if (s1.length() > 2)
					 {
						s1 = s1.substring(0, s1.length() - 2);
					 }
					 modCommodity.setOrderTotalPrice(s1);
					 cList.add(modCommodity);
				  }

				  else if (name.equalsIgnoreCase("PaymentType"))
				  {
					 modPayType = new ModPayType();
					 modPayType.setPayType(parser.nextText());
				  }
				  else if (name.equalsIgnoreCase("PaymnetPrice"))
				  {
					 String s1 = parser.nextText();
					 if (s1.length() > 2)
					 {
						s1 = s1.substring(0, s1.length() - 2);
					 }
					 modPayType.setPayAmount(s1);
					 pList.add(modPayType);
				  }
				  break;
			}
			eventType = parser.next();

		 }
		 modOrder.setPayTypeList(pList);
		 modOrder.setCommodityList(cList);
	  }
	  catch (MalformedURLException e1)
	  {
		 // TODO Auto-generated catch block
		 e1.printStackTrace();
	  }
	  catch (Exception e)
	  {
		 // TODO: handle exception
		 Log.e("ERROR", "Load()" + e.getMessage());
		 return null;
	  }
	  finally
	  {
		 if (is != null)
		 {
			try
			{
			   is.close();
			}
			catch (IOException e)
			{
			   // TODO Auto-generated catch block
			   e.printStackTrace();
			}
		 }
	  }
	  return null;
   }

   @Override
   protected void onPreExecute()
   {
	  // TODO Auto-generated method stub
	  dialogs.ShowLoadingDialog("获取订单信息中，请稍候。。。", "流水号:" + barCode);
   }

   @Override
   protected void onPostExecute(Void result)
   {
	  // TODO Auto-generated method stub
	  dialogs.setDialogDismiss();
	  if (!isCancelled())
	  {
		 try
		 {
			if (modOrder == null || modOrder.getOrderCode() == null)
			{
			   dialogs.ShowOKAlertDialog("提示", "没有找到此订单或网络故障");
			}
			else
			{
			   // searchResult = modOrder.getSerialNumber();
			   intent.putExtra("modOrder", modOrder);
			   Activity activity = (Activity) context;
			   activity.startActivityForResult(intent, activityCode);
			   
			}
		 }
		 catch (Exception e)
		 {
			// TODO: handle exception
			dialogs.ShowOKAlertDialog("提示", "没有找到此订单或网络故障");
		 }
	  }
   }

   @Override
   protected void onCancelled()
   {
	  // TODO Auto-generated method stub
	  super.onCancelled();
   }
}
